-- UIGuideBuyEquip
-- Create by zouyb
-- 引导购买装备

--UIGuideBuyEquip继承自Layer
UIGuideBuyEquip = class("UIGuideBuyEquip", function()
    return cc.Layer:create();
end);

function UIGuideBuyEquip.create(itemId)
    return UIGuideBuyEquip.new(itemId);
end

local resize;

--构造函数
function UIGuideBuyEquip:ctor(itemId)
    self:setName("UIGuideBuyEquip");
    self.itemId = itemId;

    --初始化
    local node = cc.CSLoader:createNode("layout/workshop/StrengthenDetail.csb");
    self:addChild(node);
    self.node = node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIGuideBuyEquip");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册购买按钮点击事件
    local btnBuy = findChildByName(self.node, "CT/strengthen");
    local function onBuyClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local curMoney = ME.user.dbase:query("money", 0);
            local needMoney = FormulaM.invoke("GET_WORKSHOP_BUY_EQUIP_PRICE", ME.user);
            if curMoney < needMoney then
                alert(getLocStr("not_enough_money"));
                return;
            end

            Operation.cmd_workshop_buy_equip();
            Operation.cmd_set_workshop_guide_progress(8);
        end
    end
    btnBuy:addTouchEventListener(onBuyClick);
    TextStyleM.setTextStyle(btnBuy, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBuy:setTitleText(getLocStr("btn_text_buy"));

    -- 标题
    local titleLabel = findChildByName(node, "CT/template/title");
    titleLabel:setString(getLocStr("workshop_buy_equip"));
    TextStyleM.setTitleStyle(titleLabel);

    -- 道具描述1
    local desc1Label = findChildByName(self.node, "CT/desc1");
    TextStyleM.setTextStyle(desc1Label,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(desc1Label, getLocStr("workshop_buy_equip_desc1"));

    -- 图标
    local item = findChildByName(self.node, "CT/item");
    local icon = item:getChildByName("icon");
    icon:loadTexture(getWorkshopStrengthenIconPath("equip"));

    local frame = item:getChildByName("frame");
    frame:loadTexture(getWorkshopStrengthenIconPath("bg4"));

    -- 等级
    local levelLabel = item:getChildByName("level");
    levelLabel:setVisible(false);

    -- 花费金币
    local costLabel = findChildByName(self.node, "CT/cost");
    local amount = FormulaM.invoke("GET_WORKSHOP_BUY_EQUIP_PRICE", ME.user);
    local curMoney = ME.user.dbase:query("money", 0);
    local costStr;
    if curMoney < amount then
        costStr = getLocStr("workshop_buy_equip_cost") .. " [red]" .. tostring(amount) .."[-]";
    else
        costStr = getLocStr("workshop_buy_equip_cost") .. " [blue]" .. tostring(amount) .."[-]";
    end
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false);
    applyString(costLabel, costStr);

    -- 调整高度
    self:adjustHeight();

    resize(self);
end

-- 调整高度
function UIGuideBuyEquip:adjustHeight()
    local costLabel = findChildByName(self.node, "CT/cost");
    local lineNum = costLabel:getVirtualRenderer():getStringNumLines();

    local delta = (3 - lineNum) * TextStyleM.TEXT_SIZE_SMALL;
    if delta == 0 then
        return;
    end

    costLabel:setPositionY(costLabel:getPositionY() + delta / 2);

    local btnBuy = findChildByName(self.node, "CT/strengthen");
    btnBuy:setPositionY(btnBuy:getPositionY() + delta);

    local bg = findChildByName(self.node, "CT/template/bg");

    local bgDelta = delta / bg:getScaleY();
    local bg3 = findChildByName(self.node, "CT/template/bg/bg3");
    bg3:setPositionY(bg3:getPositionY() + bgDelta );
    local bg4 = findChildByName(self.node, "CT/template/bg/bg4");
    bg4:setPositionY(bg4:getPositionY() + bgDelta);
end

resize = function(self)
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


